package com.ruoyi.system.service.impl.processServiceImpl;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.process.ProcessAluminizedRibbon;
import com.ruoyi.system.domain.process.ProcessInventory;
import com.ruoyi.system.domain.process.ProcessRibbon;
import com.ruoyi.system.domain.process.ProcessRibbonInventory;
import com.ruoyi.system.mapper.process.ProcessAluminizedRibbonMapper;
import com.ruoyi.system.mapper.process.ProcessInventoryMapper;
import com.ruoyi.system.mapper.process.ProcessRibbonInventoryMapper;
import com.ruoyi.system.mapper.process.ProcessRibbonMapper;
import com.ruoyi.system.service.processService.IProcessRibbonService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 并卷Service业务层处理
 * 
 * @author ljx
 * @date 2020-07-07
 */
@Service
public class ProcessRibbonServiceImpl implements IProcessRibbonService
{
    @Resource
    private ProcessRibbonMapper processRibbonMapper;
    @Resource
    private ProcessRibbonInventoryMapper processRibbonInventoryMapper;
    @Resource
    private ProcessInventoryMapper processInventoryMapper;
    @Resource
    private ProcessAluminizedRibbonMapper processAluminizedRibbonMapper;

    /**
     * 查询并卷
     * 
     * @param id 并卷ID
     * @return 并卷
     */
    @Override
    public ProcessRibbon selectProcessRibbonById(Long id)
    {
        return processRibbonMapper.selectProcessRibbonById(id);
    }

    /**
     * 查询并卷列表
     * 
     * @param processRibbon 并卷
     * @return 并卷
     */
    @Override
    public List<ProcessRibbon> selectProcessRibbonList(ProcessRibbon processRibbon)
    {
        return processRibbonMapper.getRibbonWithInventory(processRibbon);
    }

    @Override
    public List<ProcessRibbon> selectRibbonAndInventory(ProcessRibbon processRibbon)
    {
        //获得到所有的并卷
        List<ProcessRibbon> ribbons = processRibbonMapper.getRibbonWithInventory(processRibbon);
        //获得镀铝已经有的并卷
//        Set<Long> ribbonIds = processAluminizedRibbonMapper.selectProcessAluminizedRibbonList(null)
//                .stream().map(ProcessAluminizedRibbon::getRibbonId).collect(Collectors.toSet());
//        ribbons = ribbons.stream().filter(r -> !ribbonIds.contains(r.getId())).collect(Collectors.toList());
//        ribbons = this.getInventories(ribbons);
        List<ProcessRibbon> ribbons2 = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(ribbons)){
            for (ProcessRibbon ribbon : ribbons) {
                List<ProcessInventory> inventories = processInventoryMapper.selectInventoryByRibbonId(ribbon.getId());
                inventories = inventories.stream().filter(i -> i.getStatus()==2).collect(Collectors.toList());
                //过滤掉出库的原膜
                if(CollectionUtils.isNotEmpty(inventories)){
                    ribbons2.add(ribbon);
                    ribbon.setInventories(inventories);
                }
            }
        }
        return ribbons2;
    }

    @Override
    public List<ProcessRibbon> selectRibbonAndInventory2(ProcessRibbon processRibbon)
    {
        //获得到所有的并卷
        List<ProcessRibbon> ribbons = processRibbonMapper.getRibbonWithInventory(processRibbon);
        //获得镀铝已经有的并卷
        Set<Long> ribbonIds = processAluminizedRibbonMapper.selectProcessAluminizedRibbonList(null)
                .stream().map(ProcessAluminizedRibbon::getRibbonId).collect(Collectors.toSet());
        ribbons = ribbons.stream().filter(r -> !ribbonIds.contains(r.getId())).collect(Collectors.toList());
        ribbons = this.getInventories(ribbons);
        return ribbons;
    }

    @Override
    public List<ProcessRibbon> selectByAluminizedId(Long id) {
        List<ProcessRibbon> ribbons = processRibbonMapper.selectByAluminizedId(id);
        ribbons = this.getInventories(ribbons);
        return ribbons;
    }

    @Override
    public List<ProcessRibbon> getInventories(List<ProcessRibbon> ribbons){
        List<ProcessRibbon> ribbons2 = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(ribbons)){
            for (ProcessRibbon ribbon : ribbons) {
                List<ProcessInventory> inventories = processInventoryMapper.selectInventoryByRibbonId(ribbon.getId());
                //过滤掉出库的原膜
                if(CollectionUtils.isNotEmpty(inventories)){
                    ribbon.setInventories(inventories);
                    ribbons2.add(ribbon);
                }
            }
        }
        return ribbons2;
    }

    /**
     * 新增并卷
     * 
     * @param processRibbon 并卷
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProcessRibbon(ProcessRibbon processRibbon)
    {
        processRibbon.setCreateTime(DateUtils.getNowDate());
        //添加并卷标数据
        processRibbonMapper.insertProcessRibbon(processRibbon);
        //添加关联表数据
        this.add(processRibbon);
        return 1;
    }

    public void add(ProcessRibbon processRibbon){
        //添加关联表数据
        if(StringUtils.isNotEmpty(processRibbon.getInventoryIds())){
            List<String> list = Arrays.asList(processRibbon.getInventoryIds().split(","))
                    .stream().distinct().collect(Collectors.toList());
            //插入并卷和加工库存关联表
            processRibbonInventoryMapper.insertList(processRibbon.getId(),list);
            //修改加工库存表的加工状态为并卷
            processInventoryMapper.updateStatusString(2,list);
        }
    }

    /**
     * 修改并卷
     * 
     * @param processRibbon 并卷
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProcessRibbon(ProcessRibbon processRibbon)
    {
        processRibbon.setUpdateTime(DateUtils.getNowDate());
        if(processRibbon.getSign()==2){
            //查询历史关联关系，删除关联关系表，修改加工库存表加工状态
            ProcessRibbonInventory ribbonInventory = new ProcessRibbonInventory();
            ribbonInventory.setRibbonId(processRibbon.getId());
            List<ProcessRibbonInventory> inventories = processRibbonInventoryMapper.selectProcessRibbonInventoryList(ribbonInventory);
            if(CollectionUtils.isNotEmpty(inventories)){
                //删除关联关系
                List<Long> ids = inventories.stream().map(ProcessRibbonInventory::getId).collect(Collectors.toList());
                processRibbonInventoryMapper.deleteByIds(ids);
                //修改加工状态
                List<Long> inventoryIds = inventories.stream().map(ProcessRibbonInventory::getInventoryId).collect(Collectors.toList());
                processInventoryMapper.updateStatus(1,inventoryIds);
            }
            //添加新关联关系，修改加工存库表加工状态
            this.add(processRibbon);
        }
        return processRibbonMapper.updateProcessRibbon(processRibbon);
    }

    /**
     * 删除并卷对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public AjaxResult deleteProcessRibbonByIds(String ids)
    {
        String[] strings = Convert.toStrArray(ids);
        List<String> delete = new ArrayList<>();
        Boolean flag = false;
        ProcessRibbonInventory ribbonInventory = new ProcessRibbonInventory();
        //修改加工库存表加工状态
        for (String s : strings) {
            ribbonInventory.setRibbonId(Long.valueOf(s));
            List<ProcessInventory> inventories = processInventoryMapper.selectInventoryByRibbonId(Long.valueOf(s));
            List<ProcessInventory> list = inventories.stream().filter(p -> p.getStatus() > 2).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(list)){
                flag = true;
                continue;
            }
            delete.add(s);
            List<Long> inventorieIds = inventories.stream().map(ProcessInventory::getId).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(inventorieIds)){
                //修改加工状态
                processInventoryMapper.updateStatus(1,inventorieIds);
            }
        }
        if(CollectionUtils.isNotEmpty(delete)){
            //删除关联表数据
            String[] de = new String[delete.size()];
            de = delete.toArray(de);
            processRibbonInventoryMapper.deleteByRibbonIds(de);
            processRibbonMapper.deleteProcessRibbonByIds(de);
        }
        if(flag){
            return AjaxResult.success("原膜已加工无法删除");
        }
        return AjaxResult.success();
    }

    /**
     * 删除并卷信息
     * 
     * @param id 并卷ID
     * @return 结果
     */
    @Override
    public int deleteProcessRibbonById(Long id)
    {
        return processRibbonMapper.deleteProcessRibbonById(id);
    }

    @Override
    public List<ProcessInventory> getProcessInventory(Long id) {
        //获得到并卷已绑定的原膜编号
        return processInventoryMapper.selectInventoryByRibbonId(id);
    }
}
